<?php

namespace App\Admin\Actions\ShopOrder;

use App\Library\Logic\LogisticsLogic;
use App\Library\Logic\OpenIdLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\PaymentLogic;
use App\Library\Logic\PaymentRelatedLogic;
use App\Library\Logic\ShopOrderGoodsLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Service\WxMessageService;
use App\Models\ShopOrderModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopOrderDeliverAction extends RowAction
{
    public $name = '发货';

    public function handle(ShopOrderModel $model, Request $request): Response
    {
        if ((int)$model->order_is_self_pickup === 1) {
            return $this->response()->error(__('errorCode.629'))->refresh();
        } else {
            if ($model->ext->status_confirm) {
                return $this->response()->error(__('errorCode.630'))->refresh();
            } else {
                if ($model->ext->status_cancel) {
                    return $this->response()->error(__('errorCode.631'))->refresh();
                }
            }
        }
        $relInfo = PaymentRelatedLogic::query()->where([
            'status_pay' => 1,
            'status_refund' => 0,
            'object_type' => 'shop_order',
            'object_id' => $model->order_id,
        ])->first();
        if (!empty($relInfo)) {
            $payInfo = PaymentLogic::query()->where([
                'payment_related_id' => $relInfo->id,
                'refund' => null,
                'status_refund' => 0,
                'object_type' => 'shop_order',
                'object_id' => $model->order_id,
                ['pay_at', '!=', null],
                ['out_trade_no', '!=', null],
            ])->first();
            if (!empty($payInfo)) {
                /*   echo "<pre>";
                   var_dump($payInfo->result['resource']['ciphertext']['payer']['openid']);
                   die;*/
                $orderData = [];
                $goodsName = ShopProductLogic::query()
                    ->where('product_id', $model->product_id)
                    ->value('product_name');
                $orderData['order_key']['order_number_type'] = 2;
                $orderData['order_key']['transaction_id'] = $payInfo['out_trade_no'];
                $orderData['delivery_mode'] = 1;
                $orderData['logistics_type'] = 1;
                $orderData['upload_time'] = $payInfo->result['resource']['ciphertext']['success_time'];
                $orderData['shipping_list'][0]['item_desc'] = $goodsName . '*' . $model->order_product_num;
                $orderData['payer']['openid'] = $payInfo->result['resource']['ciphertext']['payer']['openid'];
                /*  $res= WxMessageService::miNiDelivery($orderData);
                  echo "<pre>";var_dump($res);die;*/
            }
        }

        /*echo 2222;die;*/
        $info = LogisticsLogic::findByWhere([
            'expressNo' => $request->expressNo, 'objectType' => 1, 'object_id' => $model->order_id,
        ]);
        if (is_null($info)) {
            LogisticsLogic::insertForModel([
                'objectType' => 1,
                'objectId' => $model->order_id,
                'expressNo' => $request->expressNo,
                'type' => 0,
            ]);
        }

        $model->ext->deliver_at = now()->toDateTimeString();
        $model->ext->status = 3;
        $model->ext->status_deliver = 1;
        $model->ext->save();
        mq('shopOrderConfirm', $model->serial_number);
        OperateLogLogic::add(Admin::user()->user_id, 'admin.shopOrder.deliver');
        $goodsName = ShopOrderGoodsLogic::query()
            ->where('order_id', $model->order_id)
            ->value('title');
        $goodsData = [
            'goods_name' => $goodsName,
            'time' => date('Y年m月d日 H:i', time()),
            'goods_order_no' => $model->serial_number,
            'deliver_unit' => 'BOXTRIP',
            'logistics_no' => $request->expressNo
        ];
        $userInfo = OpenIdLogic::query()
            ->where('user_id',$model->buy_user_id)->first();
        WxMessageService::goodsWxMess($goodsData, $userInfo, [], 3);
        return $this->response()->success('操作成功！')->refresh();
    }

    public function form(): void
    {
        $this->text('expressNo', __('shop_order.ed_sn'))->attribute('maxlength', 20)->required();
    }
}
